package com.dongrj.framework.web;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Converter<One, Two> {
    private final Function<One, Two> oneToTwoFunction;
    private final Function<Two, One> twoToOneFunction;

    public Converter(Function<One, Two> oneToTwoFunction, Function<Two, One> twoToOneFunction) {
        this.oneToTwoFunction = oneToTwoFunction;
        this.twoToOneFunction = twoToOneFunction;
    }

    public Two toTwo(One one) {
        return oneToTwoFunction.apply(one);
    }

    public One toOne(Two two) {
        return twoToOneFunction.apply(two);
    }

    public List<Two> toTwoList(Collection<One> ones) {
        return ones.stream().map(this::toTwo).collect(Collectors.toList());
    }

    public List<One> toOneList(Collection<Two> twos) {
        return twos.stream().map(this::toOne).collect(Collectors.toList());
    }
}